<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 公海
 * @icon fa fa-user
 */
class Highsea extends Backend
{
    protected $relationSearch = true;
    protected $SourceModel = null;
    protected $ReceiveModel = null;
    protected $AdminModel = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Business.Business');
        $this->SourceModel = model('Business.Source');
        $this->ReceiveModel = model('Business.Receive');
        $this->AdminModel = model('Admin');
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('scource')
                ->where($where)
                ->where('adminid', null)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('scource')
                ->where($where)
                ->where('adminid', null)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    // 领取
    public function apply($ids = "")
    {
        $ids = explode(",", $ids);
        $datas = [];
        $ReceiveData = [];
        foreach ($ids as $id) {
            $datas[] = [
                'id' => $id,
                'adminid' => $this->auth->id
            ];
            $ReceiveData[] = [
                'busid' => $id,
                'applyid' => $this->auth->id,
                'status' => 'apply',

            ];
        }
        // 开启事务
        $this->model->startTrans();
        $this->ReceiveModel->startTrans();
        $result = $this->model->isUpdate()->saveAll($datas);
        if ($result === false) {
            $this->error('操作失败');
        }
        $Receiveresult = $this->ReceiveModel->validate('common/Business/Receive')->saveAll($ReceiveData);
        if ($Receiveresult === false) {
            $this->model->rollback();
            $this->error('操作失败');
        }
        $this->model->commit();
        $this->ReceiveModel->commit();
        $this->success('操作成功');
    }
    // 获取来源
    public function source()
    {
        $list = $this->SourceModel->order('id', 'desc')->select();
        $data = [];
        foreach ($list as $item) {
            $data[$item['name']] = $item['name'];
        }
        echo json_encode($data);
    }
    public function recovery($ids=null)
    {
        $row = ['ids' => $ids];
        $ids = explode(",", $ids);
        $subNamelist = $this->model->where('id', 'IN', $ids)->column('nickname');
        $adminlist = $this->AdminModel->column('nickname', 'id');
        $this->assign(['row' => $row, 'sublist' => $subNamelist, 'adminlist' => $adminlist]);
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $ids = explode(",", $params['ids']);
            $datas = [];
            $ReceiveData = [];
            foreach ($ids as $id) {
                $datas[] = [
                    'id' => $id,
                    'adminid' => $params['adminid']
                ];
                $ReceiveData[] = [
                    'busid' => $id,
                    'applyid' => $params['adminid'],
                    'status' => 'allot',
    
                ];
            }
            // 开启事务
            $this->model->startTrans();
            $this->ReceiveModel->startTrans();
            $result = $this->model->isUpdate()->saveAll($datas);
            if ($result === false) {
                $this->error('操作失败');
            }
            $Receiveresult = $this->ReceiveModel->validate('common/Business/Receive')->saveAll($ReceiveData);
            if ($Receiveresult === false) {
                $this->model->rollback();
                $this->error('操作失败');
            }
            $this->model->commit();
            $this->ReceiveModel->commit();
            $this->success('操作成功');
        }
        return $this->view->fetch();
    }
    public function del($ids=null){
        $row = $this->model->get($ids);
        if(!$row){
            $this->error(__('用户信息不存在'));
        }
        $result=$this->model->destroy($ids);
        if($result===false){
            $this->error("操作失败");
        }
        $this->success("操作成功");
    }
}
